package cn.st4rlight.util.report;

import java.util.List;
import java.util.Map;
import java.util.function.Predicate;

import cn.st4rlight.util.check.DataCheckResult;
import cn.st4rlight.util.collection.TransformUtil;
import cn.st4rlight.util.json.ObjectMapperUtil;
import cn.st4rlight.util.value.SafeUtil;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

/**
 * @author st4rlight <st4rlight@163.com>
 * Created on 2025-05-03
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ReportUtil {

    /**
     * 一个公共校验方法
     * 一堆remainItems, 使用validator校验, 不通过的放入resultList中，理由是errMsg，通过的作为返回值返回
     */
    public static <T> List<T> filterByValidator(List<T> remainItems,
        Predicate<T> validator, List<DataCheckResult<T>> resultList, String errMsg
    ) {
        Map<Boolean, List<T>> validateResultMap = TransformUtil.groupBy2List(remainItems, validator::test);
        SafeUtil.of(validateResultMap.get(false)).forEach(item -> resultList.add(DataCheckResult.reject(item, errMsg)));
        return SafeUtil.of(validateResultMap.get(true));
    }

    /**
     * 行字段map转对象
     */
    public static <T>List<T> mapRowMapList(List<Map<String, String>> rowMapList, Class<T> clazz) {
        return TransformUtil.toList(rowMapList, rowMap -> ObjectMapperUtil.mapper().convertValue(rowMap, clazz));
    }
}
